package com.departmentstore.controller;

import com.departmentstore.dto.ManufacturerProductDTO;
import com.departmentstore.entity.ManufacturerProduct;
import com.departmentstore.service.ManufacturerProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/manufacturer-products")
@Tag(name = "制造商商品管理", description = "制造商商品的增删改查接口")
public class ManufacturerProductController {

    private final ManufacturerProductService manufacturerProductService;

    @Autowired
    public ManufacturerProductController(ManufacturerProductService manufacturerProductService) {
        this.manufacturerProductService = manufacturerProductService;
    }

    @PostMapping
    @Operation(summary = "创建制造商商品", description = "创建一个新的制造商商品关联")
    public ResponseEntity<ManufacturerProduct> createManufacturerProduct(@Valid @RequestBody ManufacturerProduct manufacturerProduct) {
        return ResponseEntity.ok(manufacturerProductService.createManufacturerProduct(manufacturerProduct));
    }

    @PutMapping("/{manufacturerId}/{productId}")
    @Operation(summary = "更新制造商商品", description = "根据制造商ID和商品ID更新制造商商品信息")
    public ResponseEntity<ManufacturerProduct> updateManufacturerProduct(
            @Parameter(description = "制造商ID") @PathVariable String manufacturerId,
            @Parameter(description = "商品ID") @PathVariable String productId,
            @Valid @RequestBody ManufacturerProduct manufacturerProduct) {
        manufacturerProduct.setManufacturerId(manufacturerId);
        manufacturerProduct.setProductId(productId);
        return ResponseEntity.ok(manufacturerProductService.updateManufacturerProduct(manufacturerProduct));
    }

    @DeleteMapping("/{manufacturerId}/{productId}")
    @Operation(summary = "删除制造商商品", description = "根据制造商ID和商品ID删除制造商商品关联")
    public ResponseEntity<Void> deleteManufacturerProduct(
            @Parameter(description = "制造商ID") @PathVariable String manufacturerId,
            @Parameter(description = "商品ID") @PathVariable String productId) {
        manufacturerProductService.deleteManufacturerProduct(manufacturerId + "_" + productId);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/{manufacturerId}/{productId}")
    @Operation(summary = "获取制造商商品", description = "根据制造商ID和商品ID获取制造商商品信息")
    public ResponseEntity<ManufacturerProduct> getManufacturerProduct(
            @Parameter(description = "制造商ID") @PathVariable String manufacturerId,
            @Parameter(description = "商品ID") @PathVariable String productId) {
        return ResponseEntity.ok(manufacturerProductService.getManufacturerProductById(manufacturerId + "_" + productId));
    }

    @GetMapping
    @Operation(summary = "获取所有制造商商品", description = "获取所有制造商商品的列表")
    public ResponseEntity<List<ManufacturerProductDTO>> getAllManufacturerProducts() {
        return ResponseEntity.ok(manufacturerProductService.getAllManufacturerProducts());
    }

    @GetMapping("/manufacturer/{manufacturerId}")
    @Operation(summary = "根据制造商ID查找商品", description = "根据制造商ID查找该制造商的所有商品")
    public ResponseEntity<List<ManufacturerProduct>> getManufacturerProductsByManufacturerId(
            @Parameter(description = "制造商ID") @PathVariable String manufacturerId) {
        return ResponseEntity.ok(manufacturerProductService.getManufacturerProductsByManufacturerId(manufacturerId));
    }

    @GetMapping("/product/{productId}")
    @Operation(summary = "根据商品ID查找制造商", description = "根据商品ID查找生产该商品的所有制造商")
    public ResponseEntity<List<ManufacturerProduct>> getManufacturerProductsByProductId(
            @Parameter(description = "商品ID") @PathVariable String productId) {
        return ResponseEntity.ok(manufacturerProductService.getManufacturerProductsByProductId(productId));
    }

    @GetMapping("/manufacturer/{manufacturerId}/product/{productId}")
    public ResponseEntity<ManufacturerProduct> getManufacturerProductByManufacturerIdAndProductId(
            @PathVariable String manufacturerId,
            @PathVariable String productId) {
        return ResponseEntity.ok(manufacturerProductService.getManufacturerProductByManufacturerIdAndProductId(manufacturerId, productId));
    }
} 